package cc.rengu.igas.bsps.biz.service.impl;

import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.facade.MchntQueryService;
import cc.rengu.igas.bsps.facade.request.*;
import cc.rengu.igas.bsps.facade.response.*;
import cc.rengu.igas.bsps.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * 商户服务查询类功能.
 */
public class MchntQueryServiceImpl implements MchntQueryService {

    RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<MarketAdvertQueryResponse> marketAdvertQuery(MarketAdvertQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MarketAdvertQueryResponse> bspResult = new Result<>();
        MarketAdvertQueryResponse marketAdvertQueryResponse = new MarketAdvertQueryResponse();
        marketAdvertQueryResponse.setHeader(request.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(request, MarketAdvertQueryResponse.class);
            marketAdvertQueryResponse = (MarketAdvertQueryResponse) dubboResult.getResult();
            marketAdvertQueryResponse.setRespCode(dubboResult.getRespCode());
            marketAdvertQueryResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("营销轮播广告查询失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                marketAdvertQueryResponse.setRespCode(((BizException) e).getCode());
                marketAdvertQueryResponse.setRespDesc(e.getMessage());
            } else {
                marketAdvertQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                marketAdvertQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(marketAdvertQueryResponse);
        return bspResult;
    }

    @Override
    public Result<CurrentTransStatisInfoQueryResponse> currentTransStatisInfoQuery(CurrentTransStatisInfoQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CurrentTransStatisInfoQueryResponse> bspResult = new Result<>();
        CurrentTransStatisInfoQueryResponse currentTransStatisInfoQueryResponse = new CurrentTransStatisInfoQueryResponse();
        currentTransStatisInfoQueryResponse.setHeader(request.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(request, CurrentTransStatisInfoQueryResponse.class);
            currentTransStatisInfoQueryResponse = (CurrentTransStatisInfoQueryResponse) dubboResult.getResult();
            currentTransStatisInfoQueryResponse.setRespCode(dubboResult.getRespCode());
            currentTransStatisInfoQueryResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("当日收款信息查询失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                currentTransStatisInfoQueryResponse.setRespCode(((BizException) e).getCode());
                currentTransStatisInfoQueryResponse.setRespDesc(e.getMessage());
            } else {
                currentTransStatisInfoQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                currentTransStatisInfoQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(currentTransStatisInfoQueryResponse);
        return bspResult;
    }

    @Override
    public Result<MchntAcctBalanceQueryResponse> mchntAcctBalanceQuery(MchntAcctBalanceQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntAcctBalanceQueryResponse> bspResult = new Result<>();
        MchntAcctBalanceQueryResponse mchntAcctBalanceQueryResponse = new MchntAcctBalanceQueryResponse();
        mchntAcctBalanceQueryResponse.setHeader(request.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(request, MchntAcctBalanceQueryResponse.class);
            mchntAcctBalanceQueryResponse = (MchntAcctBalanceQueryResponse) dubboResult.getResult();
            mchntAcctBalanceQueryResponse.setRespCode(dubboResult.getRespCode());
            mchntAcctBalanceQueryResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户待结算余额查询失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                mchntAcctBalanceQueryResponse.setRespCode(((BizException) e).getCode());
                mchntAcctBalanceQueryResponse.setRespDesc(e.getMessage());
            } else {
                mchntAcctBalanceQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntAcctBalanceQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(mchntAcctBalanceQueryResponse);
        return bspResult;
    }

    @Override
    public Result<MchntDiscountSurplusQuotaQueryResponse> mchntDiscountSurplusQuotaQuery(MchntDiscountSurplusQuotaQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntDiscountSurplusQuotaQueryResponse> bspResult = new Result<>();
        MchntDiscountSurplusQuotaQueryResponse mchntDiscountSurplusQuotaQueryResponse = new MchntDiscountSurplusQuotaQueryResponse();
        mchntDiscountSurplusQuotaQueryResponse.setHeader(request.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(request, MchntDiscountSurplusQuotaQueryResponse.class);
            mchntDiscountSurplusQuotaQueryResponse = (MchntDiscountSurplusQuotaQueryResponse) dubboResult.getResult();
            mchntDiscountSurplusQuotaQueryResponse.setRespCode(dubboResult.getRespCode());
            mchntDiscountSurplusQuotaQueryResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户优惠剩余额度查询失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                mchntDiscountSurplusQuotaQueryResponse.setRespCode(((BizException) e).getCode());
                mchntDiscountSurplusQuotaQueryResponse.setRespDesc(e.getMessage());
            } else {
                mchntDiscountSurplusQuotaQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntDiscountSurplusQuotaQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(mchntDiscountSurplusQuotaQueryResponse);
        return bspResult;
    }

    @Override
    public Result<MchntQuotaInfoQueryResponse> mchntQuotaInfoQuery(MchntQuotaInfoQueryRequest mchntQuotaInfoQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntQuotaInfoQueryResponse> bspResult = new Result<>();
        MchntQuotaInfoQueryResponse mchntQuotaInfoQueryResponse = new MchntQuotaInfoQueryResponse();
        mchntQuotaInfoQueryResponse.setHeader(mchntQuotaInfoQueryRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntQuotaInfoQueryRequest, MchntQuotaInfoQueryResponse.class);
            mchntQuotaInfoQueryResponse = (MchntQuotaInfoQueryResponse) dubboResult.getResult();
            mchntQuotaInfoQueryResponse.setRespCode(dubboResult.getRespCode());
            mchntQuotaInfoQueryResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户限额查询失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                mchntQuotaInfoQueryResponse.setRespCode(((BizException) e).getCode());
                mchntQuotaInfoQueryResponse.setRespDesc(e.getMessage());
            } else {
                mchntQuotaInfoQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntQuotaInfoQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(mchntQuotaInfoQueryResponse);
        return bspResult;
    }

    @Override
    public Result<MchntTransStatisInfoQueryResponse> mchntTransStatisInfoQuery(MchntTransStatisInfoQueryRequest mchntTransStatisInfoQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntTransStatisInfoQueryResponse> bspResult = new Result<>();
        MchntTransStatisInfoQueryResponse mchntTransStatisInfoQueryResponse = new MchntTransStatisInfoQueryResponse();
        mchntTransStatisInfoQueryResponse.setHeader(mchntTransStatisInfoQueryRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntTransStatisInfoQueryRequest, MchntTransStatisInfoQueryResponse.class);
            mchntTransStatisInfoQueryResponse = (MchntTransStatisInfoQueryResponse) dubboResult.getResult();
            mchntTransStatisInfoQueryResponse.setRespCode(dubboResult.getRespCode());
            mchntTransStatisInfoQueryResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("巡检任务审核操作失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                mchntTransStatisInfoQueryResponse.setRespCode(((BizException) e).getCode());
                mchntTransStatisInfoQueryResponse.setRespDesc(e.getMessage());
            } else {
                mchntTransStatisInfoQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntTransStatisInfoQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(mchntTransStatisInfoQueryResponse);
        return bspResult;
    }

    @Override
    public Result<EventProcLogQueryResponse> eventProcLogQuery(EventProcLogQueryRequest eventProcLogQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<EventProcLogQueryResponse> bspResult = new Result<>();
        EventProcLogQueryResponse eventProcLogQueryResponse = new EventProcLogQueryResponse();
        eventProcLogQueryResponse.setHeader(eventProcLogQueryRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(eventProcLogQueryRequest, EventProcLogQueryResponse.class);
            eventProcLogQueryResponse = (EventProcLogQueryResponse) dubboResult.getResult();
            eventProcLogQueryResponse.setRespCode(dubboResult.getRespCode());
            eventProcLogQueryResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("工单处理明细查询失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                eventProcLogQueryResponse.setRespCode(((BizException) e).getCode());
                eventProcLogQueryResponse.setRespDesc(e.getMessage());
            } else {
                eventProcLogQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                eventProcLogQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(eventProcLogQueryResponse);
        return bspResult;
    }

    @Override
    public Result<VoiceDevBindListQueryResponse> voiceDevBindListQuery(VoiceDevBindListQueryRequset voiceDevBindListQueryRequset) {
        DubboService dubboService = new DubboServiceImpl();
        Result<VoiceDevBindListQueryResponse> bspResult = new Result<>();
        VoiceDevBindListQueryResponse voiceDevBindListQueryResponse = new VoiceDevBindListQueryResponse();
        voiceDevBindListQueryResponse.setHeader(voiceDevBindListQueryRequset.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(voiceDevBindListQueryRequset, VoiceDevBindListQueryResponse.class);
            voiceDevBindListQueryResponse = (VoiceDevBindListQueryResponse) dubboResult.getResult();
            voiceDevBindListQueryResponse.setRespCode(dubboResult.getRespCode());
            voiceDevBindListQueryResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("查询语音设备绑定列表失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                voiceDevBindListQueryResponse.setRespCode(((BizException) e).getCode());
                voiceDevBindListQueryResponse.setRespDesc(e.getMessage());
            } else {
                voiceDevBindListQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                voiceDevBindListQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(voiceDevBindListQueryResponse);
        return bspResult;
    }

    @Override
    public Result<MchntBindListQueryResponse> mchntBindListQuery(MchntBindListQueryRequset mchntBindListQueryRequset) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntBindListQueryResponse> bspResult = new Result<>();
        MchntBindListQueryResponse mchntBindListQueryResponse = new MchntBindListQueryResponse();
        mchntBindListQueryResponse.setHeader(mchntBindListQueryRequset.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntBindListQueryRequset, MchntBindListQueryResponse.class);
            mchntBindListQueryResponse = (MchntBindListQueryResponse) dubboResult.getResult();
            mchntBindListQueryResponse.setRespCode(dubboResult.getRespCode());
            mchntBindListQueryResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("查询商户绑定列表信息失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                mchntBindListQueryResponse.setRespCode(((BizException) e).getCode());
                mchntBindListQueryResponse.setRespDesc(e.getMessage());
            } else {
                mchntBindListQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntBindListQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(mchntBindListQueryResponse);
        return bspResult;
    }
}
